←Select platform

WriteJavaScript(stream,string,IList<PDFJavaScript>,stream) Method

Summary

Writes (or removes) JavaScript actions to an output stream.

Syntax
C#
C++/CLI
Python
public static void WriteJavaScript( 
   Stream sourceStream, 
   string password, 
   IList<PDFJavaScript> Script_, 
   Stream destinationStream 
) 
public static void WriteJavaScript( 
   Stream^ sourceStream, 
   String^ password, 
   IList<PDFJavaScript>^ Script_, 
   Stream^ destinationStream 
) 
def WriteJavaScript(self,sourceStream,password,Script_,destinationStream): 

Parameters

sourceStream

Stream containing the source PDF.

password

The password to use if sourceStream contains an encrypted PDF.

javaScript

List of WriteJavaScript actions to be written. If null or the array is empty, the function deletes the JavaScript from the file.

destinationStream

Stream that will contain the output PDF.

Remarks

If javaScript is not null and contains valid JavaScript actions, WriteJavaScript will:

  1. Copy the PDF file from sourceStream
  2. Add the JavaScript actions
  3. Write the result to destinationStream

If the source PDF file contains JavaScript, those operations will be replaced by the actions from javaScript. To add JavaScript actions to the existing JavaScript actions from the source file, follow these steps:

  1. Call PDFFile.ExtractJavaScript(stream,string) to read the existing JavaScript actions
  2. Add your actions to the existing list
  3. Write all the actions (existing and new) to the output stream using WriteJavaScript.

If javaScript is null, WriteJavaScript will remove any JavaScript from the file.

There are several variations of this function, which take the source from a disk file or a stream and write the output to a disk file or a stream.

The actions from javaScript will be sorted in ascending order by the PDFJavaScript.Name property. This means the order in which the JavaScript actions are written to the ourput and executed by the JavaScript interpreter is not necessarily the order in which you add them to the javaScript list. See PDFJavaScript.Name for more details.

The example below demonstrates the result of this sort. The example adds two actions named name2 and name1. The write however, changes the order and writes them in the order name1, name2. So when you load the resulting PDF file in a viewer that supports JavaScript (eg: web browser), the messages are displayed in reverse order.

Example
C#
Java
using Leadtools.WinForms; 
using Leadtools; 
using Leadtools.Codecs; 
using Leadtools.Controls; 
using Leadtools.Drawing; 
using Leadtools.ImageProcessing; 
using Leadtools.Pdf; 
using Leadtools.Svg; 
 
 
public void TestWriteJavaScript_Stream_string_IList_Stream() 
{ 
   string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "leadtools.pdf"); ; 
   string dstFileName = Path.Combine(LEAD_VARS.ImagesDir, @"out.pdf"); 
   List<PDFJavaScript> list = new List<PDFJavaScript>(2); 
   PDFJavaScript item; 
 
   item.Name = "name2"; 
   item.Code = "app.alert(\"Hello 2\");"; 
   list.Add(item); 
 
   item.Name = "name1"; 
   item.Code = "app.alert(\"Hello 1\");"; 
   list.Add(item); 
 
   using (FileStream srcStream = File.Open(srcFileName, FileMode.Open)) 
   using (FileStream destStream = File.Create(dstFileName)) 
      PDFFile.WriteJavaScript(srcStream, null, list, destStream); 
} 
 
static class LEAD_VARS 
{ 
   public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images"; 
} 
 
import java.io.ByteArrayInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.nio.file.Files; 
import java.nio.file.Paths; 
import java.nio.file.StandardCopyOption; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import java.util.Scanner; 
 
import org.junit.*; 
import org.junit.runner.JUnitCore; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 
import static org.junit.Assert.*; 
 
import leadtools.*; 
import leadtools.codecs.*; 
import leadtools.pdf.*; 
 
 
public void pdfFileWriteJavaScriptStreamStream() throws IOException { 
 
   final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images"; 
   String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "leadtools.pdf"); 
   String dstFileName = combine(LEAD_VARS_IMAGES_DIR, "out.pdf"); 
   ILeadStream inputIls = LeadStreamFactory.create(srcFileName); 
   ILeadStream outputIls = LeadStreamFactory.create(dstFileName); 
 
   ArrayList<PDFJavaScript> list = new ArrayList<PDFJavaScript>(2); 
   PDFJavaScript item = new PDFJavaScript(); 
 
   item.setName("name2"); 
   item.setCode("app.alert(\"Hello 2\");"); 
   list.add(item); 
 
   item.setName("name1"); 
   item.setCode("app.alert(\"Hello 1\");"); 
   list.add(item); 
 
   PDFFile.writeJavaScript(inputIls, null, list, outputIls); 
   assertTrue("Check that new file was created", new File(dstFileName).exists()); 
} 
Requirements

Target Platforms

Help Version 23.0.2024.2.29
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Leadtools.Pdf Assembly

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.